package com.breaktian.share;

import android.content.Context;
import android.text.TextUtils;

import com.breaktian.network.download.DownLoadListener;
import com.breaktian.network.download.HttpDownload;
import com.breaktian.share.action.ShareAction;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Share {

    private final Context mContext;
    private final File mImageDir;
    private ShareEntity mShareEntity;
    private List<ShareChannel> mChannels = new ArrayList<>(6);
    private ShareCallback mCallback;

    public Share(Context context){
        this.mContext = context;
        this.mImageDir = mContext.getExternalFilesDir("shareImage");
        if(mImageDir!=null && !mImageDir.exists()){
            mImageDir.mkdirs();
        }
        Registrar.getInstance().register(this);
    }

    public static Share get(Context context){
        return new Share(context);
    }

    public void share(String title, String content, String imageUrl, String url) {
        share(new ShareEntity(title, content, url, imageUrl));
    }

    public void shareImage(String imageUrl) {
        share(new ShareEntity(null, null, null, imageUrl));
    }

    void share(final ShareEntity entry) {
        if (TextUtils.isEmpty(entry.imagePath)&&!TextUtils.isEmpty(entry.imageUrl)) {
            String filename = Utils.md5(entry.imageUrl);
            final File imageFile = new File(mImageDir, filename);
            if (imageFile.exists()) {
                entry.imagePath = imageFile.getAbsolutePath();
                share(entry);
                return;
            }
            HttpDownload.get().downloadFile(entry.imageUrl, imageFile, new DownLoadListener() {
                @Override
                public void onFail(File file) {
                    failure("下载分享图片失败");
                }

                @Override
                public void onSucess(File file) {
                    entry.imagePath = imageFile.getAbsolutePath();
                    share(entry);
                }

                @Override
                public void onProgress(long bytesRead, long contentLength, boolean done) {
                }
            });
        } else {
            showShareDialog(entry);
        }
    }


    public Share setCallback(ShareCallback callback) {
        mCallback = callback;
        return this;
    }

    public Share setChannels(String channels) {
        if (!TextUtils.isEmpty(channels)) {
            String channelsStr[] = channels.split(",");
            for (String s : channelsStr) {
                if ("1".equals(s)) {
                    addChannel(ShareChannel.TIMELINE);
                } else if ("2".equals(s)) {
                    addChannel(ShareChannel.SESSION);
                } else if ("3".equals(s)) {
                    addChannel(ShareChannel.WEIBO);
                } else if ("4".equals(s)) {
                    addChannel(ShareChannel.QQ);
                } else if ("5".equals(s)) {
                    addChannel(ShareChannel.QZONE);
                }
            }
        }
        return this;
    }

    private void showShareDialog(ShareEntity entity) {
        mShareEntity = entity;
        if (mChannels.isEmpty()) {
            addChannel(ShareChannel.TIMELINE);
            addChannel(ShareChannel.SESSION);
        }
        if(entity.isImageShare()){//目前qq空间只支持图文分享
            mChannels.remove(ShareChannel.QZONE);
        }
        new ShareDialog(mContext, this, mChannels).show();
    }

    public void addChannel(ShareChannel channel){
        mChannels.add(channel);
    }

    void share(ShareChannel channel) {
        ShareEntity entity = channel.entry != null ? channel.entry.duplicate() : mShareEntity.duplicate();

        ShareAction action = ShareAction.Factory.create(mContext, channel);
        if (action != null) {
            if (action.isAppInstalled()) {
                action.share(entity, channel);
            } else {
                failure("未安装应用");
            }
        } else {
            failure("不支持的渠道");
        }
    }

    public void cancel() {
        if (mCallback != null) mCallback.onCanceled();
        Registrar.getInstance().close();
    }

    public void failure(String msg) {
        if (mCallback != null) {
            mCallback.onFailure(msg);
        }
        Registrar.getInstance().close();
    }

    public void success() {
        if (mCallback != null) {
            mCallback.onSuccess();
        }
        Registrar.getInstance().close();
    }


    /**
     * 注册实例
     * 主要为了回调
     * */
    public static class Registrar{
        private static Registrar instance;
        private Share mShare;

        private Registrar(){}

        public static Registrar getInstance(){
            if(instance==null){
                instance = new Registrar();
            }
            return instance;
        }

        public void register(Share share){
            this.mShare = share;
        }

        public Share getShare() {
            return mShare;
        }

        void close() {
            mShare = null;
            instance = null;
        }

    }




}
